<template>
    <div>
<el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>权限管理</el-breadcrumb-item>
  <el-breadcrumb-item>角色列表</el-breadcrumb-item>
</el-breadcrumb>
<br/><br/>
 <el-card class="box-card">
    <el-button type='primary' style='float:left' @click='isshow=!isshow'>添加角色</el-button>
    <el-table
    :data="roles"
    border
    stripe>
    <el-table-column
      type='expand'
      width="150">
          <template slot-scope='scope'>
              <el-row :gutter="24" v-for='(item,index) in scope.row.children' :key="index">
              <el-col :span="4" style='text-align:center'>
                <el-tag closable @close="close1(item.id, scope.row)" >{{item.authName}}</el-tag>
              </el-col>
              <el-col :span="20"> 
                <el-row :gutter="24" v-for='(ite,ind) in item.children' :key='ind'>
                  <el-col :span="4"> 
                       <el-tag type="success" closable @close="close1(ite.id, scope.row)">{{ite.authName}}</el-tag>
                  </el-col>
                  <el-col :span="20"> 
                       <el-tag  type="warning" closable @close="close1(it.id, scope.row)" v-for='(it,indd) in ite.children' :key="indd">{{ite.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
              
              </el-row>
      
    </template>
    </el-table-column>
    <el-table-column
      prop="roleName"
      label="名称"
      width="150">
    </el-table-column>
    <el-table-column
      prop="roleDesc"
      label="描述"
      width="150">
    </el-table-column>
    <el-table-column
      fixed="right"
      label="操作">
      <template slot-scope="scope">
        <el-button type="primary" @click='edit(scope.row.id,scope.$index)' icon="el-icon-edit">编辑</el-button>
        <el-button type="danger" @click='del(scope.row.id)' icon="el-icon-delete">删除</el-button>
        <el-button type="warning" @click='set(scope.row)' icon="el-icon-setting">分配权限</el-button>
      </template>
    </el-table-column>
  </el-table>

<el-dialog title="新增角色" :visible.sync="isshow">
  <el-form :model="form" ref="addRoles">
    <el-form-item label="角色名称">
      <el-input v-model="form.roleName"></el-input>
    </el-form-item>
    <el-form-item label="角色描述">
      <el-input v-model="form.roleDesc"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="isshow = false">取 消</el-button>
    <el-button type="primary" @click='addRoles'>确 定</el-button>
  </div>
</el-dialog>


<el-dialog title="修改角色" :visible.sync="editshow">
  <el-form :model="editform" ref="editRoles">
    <el-form-item label="角色名称">
      <el-input v-model="editform.roleName"></el-input>
    </el-form-item>
    <el-form-item label="角色描述">
      <el-input v-model="editform.roleDesc"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="editshow = false">取 消</el-button>
    <el-button type="primary"  @click='editRoles'>确 定</el-button>
  </div>
</el-dialog>
 
 <el-dialog
  title="分配权限"
  :visible.sync="show"
  width="30%"
  >
  <el-tree
  :data="rightList"
  show-checkbox
  node-key="id"
  :default-expanded-keys="defKeys"
  :default-checked-keys="defKeys"
  :props="trueeProps"
  ref="tree">
</el-tree>
  <span slot="footer" class="dialog-footer">
    <el-button @click="show = false">取 消</el-button>
    <el-button type="primary" @click='setRoles'>确 定</el-button>
  </span>
</el-dialog>

</el-card>
    </div>
</template>

<script>
import {
  getRoles,
  getrights,
  addRoles,
  updaterole,
  delRoles,
  editRoles,
  addRight,
  delRights
} from "../api/api.js";
export default {
  data() {
    return {
      roles: [],
      isshow: false,
      editshow: false,
      show: false,
      edit_id: 0,
      roles_id: 0,
      rightList: [
        {
          id: 101,
          authName: "商品管理",
          path: "goods",
          pid: 0,
          children: [
            {
              id: 104,
              authName: "商品列表",
              path: "goods",
              pid: 101,
              children: [
                { id: 116, authName: "商品修改", path: "goods", pid: "104,101" }
              ]
            }
          ]
        }
      ],
      defKeys: [],
      trueeProps: {
        label: "authName",
        children: "children"
      },
      right: [],
      form: {
        roleName: "",
        roleDesc: ""
      },
      editform: {
        roleName: "",
        roleDesc: ""
      }
    };
  },
  mounted() {
    this.getRoles();
  },
  methods: {
    async getRoles() {
      let res = await getRoles(this.form);
      console.log(res);
      this.roles = res.data;
    },
    async set(role) {
      this.show = true;
      console.log("角色信息", role);
      this.roles_id = role.id;
      this.true = true;
      this.defKeys = [];
      let res = await getrights("tree");
      console.log("所有权限", res);

      if (res.meta.status == 200) {
        this.$message.success(res.meta.msg);
        this.rightList = res.data;
        console.log("当前角色下所有权限", role);
        let arr = [];
        function fn(role) {
          if (!role.children) {
            return arr.push(role.id);
          }
          role.children.forEach(item => fn(item));
        }
        fn(role);
        console.log("默认选择", arr);
        this.defKeys = arr;
      }
      this.getRoles();
    },
    async setRoles() {
      let allCheck = this.$refs.tree.getCheckedKeys();
      let halfCheck = this.$refs.tree.getHalfCheckedKeys();
      console.log("全选中的", allCheck, halfCheck);
      let arrAll = [...allCheck, ...halfCheck];
      let idStr = arrAll.join(",");
      const res = await updaterole({ id: this.roles_id, rids: idStr });
      console.log("xxx", res);
      if (res.meta.status == 200) {
        this.$message.success(res.meta.msg);
        this.getRoles();
        this.show = false;
      }
    },

    edit(id, index) {
      this.editshow = !this.editshow;
      this.editform = {
        roleName: this.roles[index].roleName,
        roleDesc: this.roles[index].roleDesc
      };
      this.edit_id = id;
    },
    editRoles() {
      this.$refs["editRoles"].validate(async valid => {
        if (valid) {
          let res = await editRoles(this.edit_id, this.editform);
          console.log(res);
          if (res.meta.status == 200) {
            this.$message.success(res.meta.msg);
            this.editshow = false;
            this.getRoles();
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    addRoles() {
      this.$refs["addRoles"].validate(async valid => {
        if (valid) {
          let res = await addRoles(this.form);
          console.log(res);
          if (res.meta.status == 201) {
            this.$message.success(res.meta.msg);
            this.isshow = false;
            this.getRoles();
          }
          this.form = {
            roleName: "",
            roleDesc: ""
          };
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    del(id) {
      this.$confirm("确定要删除吗").then(async () => {
        let res = await delRoles(id);
        if (res.meta.status == 200) {
          this.$message.success(res.meta.msg);
        }
        this.getRoles();
      });
    },


    async close1(rid,id){
      console.log(id,rid)
      let res=await delRights(id.id,rid)
      console.log(res)
      if (res.meta.status == 200) {
        this.$message.success(res.meta.msg);
        id.children = res.data;
      }
    }
  }
};
</script>

<style lang='scss' scoped >
</style>
